package cn.jhz.learn.blog.service.security.resource.file.impl;

import cn.jhz.learn.blog.component.GLOBAL;
import cn.jhz.learn.blog.component.ValidationHolder;
import cn.jhz.learn.blog.dao.file.BlogFileMapper;
import cn.jhz.learn.blog.po.file.BlogFile;
import cn.jhz.learn.blog.service.security.resource.blog.BlogValidationService;
import cn.jhz.learn.blog.service.security.resource.file.FileValidationService;

import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("fileValidationService")
public class FileValidationServiceImpl implements FileValidationService {

    private final BlogFileMapper mapper;
    private final BlogValidationService blogValidationService;

    @Autowired
    public FileValidationServiceImpl(BlogFileMapper mapper, BlogValidationService blogValidationService) {
        this.mapper = mapper;
        this.blogValidationService = blogValidationService;
    }

    @Override
        public void fileValid(Integer uid, String signatureCode) {
        blogValidationService.uidValid(uid);
            BlogFile file =
                    (BlogFile) ValidationHolder.getParam(GLOBAL.VALIDATION_KEY.CLASS_BLOG_FILE.SIGNATURE_CODE + signatureCode);

            if(file == null){
                file = mapper.selectBySignatureCode(Lists.newArrayList(signatureCode)).get(0);
                if(file != null)
                    ValidationHolder.putParam(GLOBAL.VALIDATION_KEY.CLASS_BLOG_FILE.SIGNATURE_CODE + signatureCode, file);
            }
        }
}
